<?php

require_once('config.inc.php');

function auth() {
	$cfg = get_web_auth();
	if (!isset($_SERVER['PHP_AUTH_USER']) || ($_SERVER['PHP_AUTH_USER'] != $cfg['username'] || $_SERVER['PHP_AUTH_PW'] != $cfg['password'])) {
		header('WWW-Authenticate: Basic realm="Woordenboek"');
		header('HTTP/1.0 401 Unauthorized');
		echo 'Fsck off!';
		exit;
	}
}

function xmlhead($title) {
	header("Content-type: text/html; charset=utf-8");
	?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<style type="text/css">
@import "./style/style.css";
</style>
<title><?php echo $title ?></title>
</head>
<?php
}

function xmlfoot() {
?>
<p>
<a href="http://validator.w3.org/check?uri=referer">
<img src="images/valid-xhtml11.png" alt="Valid XHTML 1.1 (almost)" height="31" width="88" />
</a>
</p>
</body>
</html>
<?php
}

function dropdown($name, $selected, $default, $options, $autosubmit = false, $optionstyles = array()) {
	echo "<select name=\"$name\"";
	if ($autosubmit) echo " onchange=\"if (this.value != '') form.submit()\"";
	echo ">\n";
	if ($default !== "") {
		echo "\t<option value=\"\">$default</option>\n";
		echo "\t<option></option>\n";
	}
	foreach (array_keys($options) as $i) {
		echo "\t<option value=\"$i\"";
		if (count($optionstyles) == count($options)) echo " style=\"{$optionstyles[$i]}\"";
		if ($i == $selected) echo " selected=\"selected\"";
		echo ">{$options[$i]}</option>\n";
	}
	echo "</select>\n";
}

function getAsocArr($table, $key, $value, $dbh, $condition = "1") {
	$retarr = array();
	$querytext =
		"SELECT $key keyname, $value val
		FROM $table
		WHERE $condition
		ORDER BY `$value`";
	$query = do_query($querytext, $dbh);
	while ($row = mysql_fetch_array($query)) {
		$retarr[$row['keyname']] = $row['val'];
	}
	return $retarr;
}

function fetchSubmittedVariable($nameOfVariable, $typeOfVariable = null) {
	if (isset($typeOfVariable)) {
		if (strtoupper($typeOfVariable) === "POST") {
			if (isset($_POST[$nameOfVariable])) {
				return $_POST[$nameOfVariable];
			}	else {
				return false;
			}
		} else if (strtoupper($typeOfVariable) === "GET") {
			if (isset($_GET[$nameOfVariable])) {
				return $_GET[$nameOfVariable];
			} else {
				return false;
			}
		} else {
			return false;
		}
	} else {
		if (isset($_POST[$nameOfVariable])) {
			return $_POST[$nameOfVariable];
		} else if (isset($_GET[$nameOfVariable])) {
			return $_GET[$nameOfVariable];
		} else {
			return false;
		}
	}
}


function initSession() {

	session_start();

	$_SESSION['sleeptime'] = 60;
	$_SESSION['test_size'] = 10;
	$_SESSION['shelfdebug'] = 1;
	$_SESSION['user_id'] = 0;
	if (!isset($_SESSION['interface_language']))
		$_SESSION['interface_language'] = 85; //85 === English
	$_SESSION['language_preference'] = array(89,85,102);
	
	if ($interface_language = fetchSubmittedVariable('interface_language') and is_numeric($interface_language)) {
		$_SESSION['interface_language'] = $interface_language; 
	} //this input field will be removed in a future version - it's here for testing purposes only now

	if ($learn_language = fetchSubmittedVariable('learn_language') and is_numeric($learn_language)) {
		$_SESSION['learn_language'] = $learn_language;
	} else if (!isset($_SESSION['learn_language'])) {
		$_SESSION['learn_language'] = 0;
	}

	if ($test_mode = fetchSubmittedVariable('test_mode') and is_numeric($test_mode)) {
		$_SESSION['test_mode'] = $test_mode;
	} else if (!isset($_SESSION['test_mode'])) {
		$_SESSION['test_mode'] = 1; //1 === translate foreign word; 2 === give foreign word for concept
	}
}

function getLanguages() {
	global $dbh, $session;
	$languages = getAsocArr(
		"language_names",
		"language_id",
		"language_name",
		$dbh,
		"name_language_id = {$_SESSION['interface_language']}" 
	);
	
	if ($_SESSION['interface_language'] != 85) {
		$languages_english = getAsocArr(
			"language_names",
			"language_id",
			"language_name",
			$dbh,
			"name_language_id = 85" 
		);
		
		foreach (array_keys($languages) as $lang) {
			unset($languages_english[$lang]);
		}
		
		if (count($languages_english) > 0) {
			$languages = $languages + array('' => '---MISSING TRANSLATIONS---') + $languages_english;
		}
		
	}
	
	return $languages;
}

function getMajorLanguages() {
	return array("85"); //TODO: find languageID's for Spanish, Mandarin, French, Portuguese, German, Italian, Dutch...
}


?>
